package com.example.testing.myapplication;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private TelephonyManager telephonyManager;
    private PhoneStateListener phoneListener;
    private final String TAG = "gnn--MainActivity";
    private String telephoneNumber;
    private EditText editText;
    private boolean isStop = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        telephonyManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
        // 开始监听
        phoneListener = new MyPhoneStateListener();
        telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        editText = (EditText) findViewById(R.id.telNumber);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 取消监听
        telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_NONE);
    }

    private void call(String number) {

        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + number);
        intent.setData(data);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        startActivity(intent);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.customCall:
                isStop = false;
                telephoneNumber = this.editText.getText().toString();
                this.call(telephoneNumber);
                break;
            case R.id.callNumber1:
                isStop = false;
                telephoneNumber = "15711055034";
                this.call(telephoneNumber);
                break;
            case R.id.callNumber2:
                isStop = false;
                telephoneNumber = "13426421951";
                this.call(telephoneNumber);
                break;
            case R.id.callNumber3:
                isStop = false;
                telephoneNumber = "18610621301";
                this.call(telephoneNumber);
                break;
            case R.id.stopCall:
                isStop = true;

                break;
        }
    }

    class MyPhoneStateListener extends PhoneStateListener {

        MyPhoneStateListener() {
            if (telephonyManager == null)
                telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        }

        public void onCallStateChanged(int state, String incomingNumber) {
            Log.e(TAG, "onCallStateChanged--" + state);

            switch (state) {
                case TelephonyManager.CALL_STATE_OFFHOOK:// 拨打电话或者接听电话时  2
                    Log.d(TAG, "state = CALL_STATE_OFFHOOK");
                    break;
                case TelephonyManager.CALL_STATE_RINGING:// 电话进来时
                    Log.d(TAG, "state = CALL_STATE_RINGING");
                    break;
                case TelephonyManager.CALL_STATE_IDLE:// 挂起电话时候，或者没有任何反映  0
                    Log.d(TAG, "state = CALL_STATE_IDLE");
                    if (!isStop && telephoneNumber != null && telephoneNumber.length() != 0)
                        call(telephoneNumber);
                    break;
                default:

                    break;
            }
        }
    }

    public class MainReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            Log.e(TAG, "onReceive: action" + action);
            if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) { // 去电

                String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            }
        }
    }
}
